package com.ylr.user.framework.resolver;

import com.ylr.user.framework.feign.client.YuUserMessageClient;
import com.ylr.user.common.bo.user_message.UserMessageBo;
import com.ylr.user.common.enums.UserMessageEnum;
import com.ylr.base.common.exception.YlrException;
import com.ylr.base.common.tool.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2023-06-08 14:03:24
 * className: YuUserMessageResolver
 * version: 1.0
 * description:
 */
@Component
public class YuUserMessageResolver {

    private static final Logger log = LoggerFactory.getLogger(YuUserMessageResolver.class);

    @Value("${spring.application.simple-name:ylr}")
    private String applicationSimpleName;

    private final YuUserMessageClient yuUserMessageClient;

    public YuUserMessageResolver(YuUserMessageClient yuUserMessageClient) {
        this.yuUserMessageClient = yuUserMessageClient;
    }

    /**
     * 保存用户消息接口
     * @param getUserId       接收用户主键
     * @param sendUserId      发送用户主键
     * @param userMessageEnum 消息类型枚举对象
     * @param message         消息内容
     */
    public void saveUserMessage(Long getUserId, Long sendUserId, UserMessageEnum userMessageEnum, String message) {
        UserMessageBo userMessageBo = new UserMessageBo();
        userMessageBo.setGetUserId(getUserId)
                .setSendUserId(sendUserId)
                .setType(userMessageEnum.getValue())
                .setTitle(userMessageEnum.getName())
                .setMessage(message);
        Result<Object> result = yuUserMessageClient.saveUserMessage(userMessageBo);
        if (result.isFail()) {
            log.warn("{}模块 => 保存用户消息操作失败。message={}", applicationSimpleName, result.getMsg());
            throw new YlrException(result);
        }
    }
}
